package com.lj.frame.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.commons.exception.YerException;
import com.lj.frame.role.entity.Role;
import com.lj.frame.role.entity.RoleMenu;
import com.lj.frame.role.model.dto.AddRoleDto;
import com.lj.frame.role.model.dto.QueryRoleListParam;
import com.lj.frame.role.repo.RoleMapper;
import com.lj.frame.role.repo.RoleMenuMapper;
import com.lj.frame.role.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@Service
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;
    private final RoleMenuMapper roleMenuMapper;
    private final MapperFactory mapperFactory;


    @Override
    public IPage<Role> queryList(QueryRoleListParam queryRoleParam) {
        LambdaQueryWrapper<Role> query = new LambdaQueryWrapper<>();
        query.like(Objects.nonNull(queryRoleParam.getName()), Role::getName, queryRoleParam.getName());
        query.eq(Objects.nonNull(queryRoleParam.getStatus()), Role::getStatus, queryRoleParam.getStatus());
        return roleMapper.selectPage(Page.of(queryRoleParam.getPage(), queryRoleParam.getRows()),query);
    }


    @Override
    public Role info(Integer id) {
        Role role = roleMapper.selectById(id);

        List<Integer> checkedList = new ArrayList<>();
        roleMenuMapper.selectList(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, id)).forEach( roleMenu -> {
            checkedList.add(roleMenu.getMenuId());
        });
        role.setCheckedIdList(checkedList);
        return role;
    }

    @Override
    public void save(AddRoleDto addRoleDto) throws YerException {
        Role role = roleMapper.selectById(addRoleDto.getId());
        if (Objects.nonNull(role)){
            role.setName(addRoleDto.getName());
            role.setMemo(addRoleDto.getMemo());
            role.setStatus(addRoleDto.getStatus());
            roleMapper.updateById(role);
        }else{
            role = mapperFactory.getMapperFacade().map(addRoleDto, Role.class);
            roleMapper.insert(role);
        }

        Integer roleId = role.getId();
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, role.getId()));
        role.getCheckedIdList().forEach( menuId ->{
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        });
        //roleMenuMapper.insertBatchSomeColumn(roleMenuList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(List<Integer> roleIdList) throws YerException {
        if (roleIdList == null || roleIdList.size() == 0){
            return;
        }
        roleMapper.deleteBatchIds(roleIdList);
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().in(RoleMenu::getRoleId, roleIdList));
    }

}
